<?php

declare(strict_types=1);

namespace Yansongda\Pay\Plugin\Sbpay;

use Closure;
use Yansongda\Pay\Contract\PluginInterface;
use Yansongda\Pay\Direction\NoHttpRequestDirection;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidConfigException;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Logger;
use Yansongda\Pay\Rocket;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Str;

use function Yansongda\Pay\verify_sbpay_sign;

class CallbackPlugin implements PluginInterface
{
    /**
     * @throws ContainerException
     * @throws InvalidConfigException
     * @throws ServiceNotFoundException
     * @throws InvalidResponseException
     */
    public function assembly(Rocket $rocket, Closure $next): Rocket
    {
        Logger::debug('[sbpay][CallbackPlugin] 插件开始装载', ['rocket' => $rocket]);
        $this->formatPayload($rocket);

        Logger::debug('[sbpay][CallbackPlugin] 插件开始装载2', ['rocket' => $rocket]);
        $params = $rocket->getParams();
        $sps_hashcode = $params['sps_hashcode'] ?? false;
        if (!$sps_hashcode) {
            throw new InvalidResponseException(Exception::INVALID_RESPONSE_SIGN, '', $params);
        }
        Logger::debug('[sbpay][CallbackPlugin] 插件开始装载1', ['params' => $params]);
        $getPaymentFields=$this->getPaymentFields();
        $getResponseFields=$this->getResponseFields();

        $payload_arr=array_merge($getPaymentFields,$getResponseFields,$rocket->getPayload()->toArray());
        Logger::debug('[sbpay][CallbackPlugin] 插件开始装载3', ['payload_arr' => $payload_arr]);

        verify_sbpay_sign($params, $payload_arr, $sps_hashcode);

        $rocket->setDirection(NoHttpRequestDirection::class)
            ->setDestination($rocket->getPayload())
        ;

        Logger::info('[sbpay][CallbackPlugin] 插件装载完毕', ['rocket' => $rocket]);

        return $next($rocket);
    }

    protected function formatPayload(Rocket $rocket): void
    {
        $payload = (new Collection($rocket->getParams()))
            ->filter(fn ($v, $k) => 'sps_hashcode' != $k && !Str::startsWith($k, '_'))
        ;

        $rocket->setPayload($payload);
    }

    public function getPaymentFields() {
        return array(
            'pay_method' => '',
            'merchant_id' => '',
            'service_id' => '',
            'cust_code' => '',
            'sps_cust_no' => '',
            'sps_payment_no' => '',
            'order_id' => '',
            'item_id' => '',
            'pay_item_id' => '',
            'item_name' => '',
            'tax' => '',
            'amount' => '',
            'pay_type' => '',
            'auto_charge_type' => '',
            'service_type' => '',
            'div_settele' => '',
            'last_charge_month' => '',
            'camp_type' => '',
            'tracking_id' => '',
            'terminal_type' => '',
            'free1' => '',
            'free2' => '',
            'free3' => '',
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getResponseFields() {
        return array(
            'request_date' => '',
            'res_pay_method' => '',
            'res_result' => '',
            'res_tracking_id' => '',
            'res_sps_cust_no' => '',
            'res_sps_payment_no' => '',
            'res_payinfo_key' => '',
            'res_payment_date' => '',
            'res_err_code' => '',
            'res_date' => '',
            'limit_second' => '',
        );
    }


}
